# frozen_string_literal: true
class InterviewsController < ApplicationController
  before_action :set_interview, only: [:update]

  def index
    conditions = {}

    start_at = Time.zone.parse(params[:start_at] || '1970-01-01')
    end_at = Time.zone.parse(params[:end_at] || Time.zone.now.to_s)
    conditions[:created_at] = start_at...end_at

    conditions[:user_code] = params[:user_code] if params[:user_code].present?
    conditions[:building_code] = params[:building_code] if params[:building_code].present?

    @interviews =
      if @current_editor.system?
        Interview
      else
        @current_editor.village.interviews
      end

    @interviews = @interviews.where(conditions).order(created_at: :desc).page(page).per(per)
  end

  def create
    @interview = Interview.new(interview_params)

    if @interview.save

      # JpushService.new(registration_id, extras, content).invoke
      redirect_to @interview, notice: 'Interview was successfully created.'
    else
      render :new
    end
  end

  def update
    if @interview.update(interview_params)
      redirect_to @interview, notice: 'Interview was successfully updated.'
    else
      render :edit
    end
  end

  private

  def set_interview
    @interview = Interview.find(params[:id])
  end

  def interview_params
    params.require(:interview).permit(:user_id, :guest_id, :leaved_at, :car_code)
  end
end
